import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/utils.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';

class DateSelect extends StatefulWidget {
  final String defaultDate;
  final Function resetDate;

  DateSelect({
    this.defaultDate,
    this.resetDate,
    @required this.onConfirm
  });

  final ValueChanged<String> onConfirm;

  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<DateSelect> {

  Widget _buildIcon() {
    String date = widget.defaultDate;

    if(date != '') {
      return Container(
        padding: EdgeInsets.all(10),
        child: GestureDetector(
          onTap: () {
            widget.resetDate();
          },
          child: Image.asset('assets/dialog_close.png', width: 16),
        )
      );
    }else {
      return Container(
        padding: EdgeInsets.all(10),
        child: Image.asset('assets/rl.png', width: 24.w),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return Container(
      width: 254.w,
      height: 40,
      child: GestureDetector(
        onTap: () {
          DatePicker.showDatePicker(
            context,
            showTitleActions: true,
            minTime: DateTime(1983, 1, 1),
            // maxTime: DateTime(2019, 6, 7),
            theme: DatePickerTheme(
              headerColor: Utils.hexToColor('#eeeeee'),
              backgroundColor: Colors.white,
              itemStyle: TextStyle(
                color: Colors.black, fontSize: 18
              ),
              doneStyle: TextStyle(
                color: Utils.hexToColor('#297CFF'), 
                fontSize: 14
              )
            ),
            onChanged: (date) {
              
            }, 
            onConfirm: (date) {
              widget.onConfirm(date.toString().substring(0, 10));
            }, 
            // currentTime: DateTime.parse(widget.date), 
            locale: LocaleType.zh
          );
        },
        child: Container(
          width: 254.w,
          height: 66.w,
          alignment: Alignment.center,
          padding: EdgeInsets.only(left: 21.w),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(4.w),
            border: Border.all(color: Utils.hexToColor('#D4D4D4'), width: 0.5), 
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(widget.defaultDate, style: TextStyle(color: Utils.hexToColor('#333333'))),
              _buildIcon()
            ],
          )
        ),
      ),
    );
  }
}